package com.example.lay.intent;

import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Lay on 2014/12/7.
 */
public class ContactsActivity extends Activity {
    private EditText eTxtNum;
    private EditText eTxtName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cantacts);
        eTxtNum = (EditText) findViewById(R.id.eTxt1);
        eTxtName = (EditText) findViewById(R.id.eTxt2);
        ((Button) findViewById(R.id.btnContact)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                //设置intent的Type
                intent.setType("vnd.android.cursor.item/phone");
                //启动Activity并希望获取返回结果
                startActivityForResult(intent, 0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (resultCode == Activity.RESULT_OK) {
                    //获取返回数据
                    Uri contactData = data.getData();
                    CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);
                    //查询联系人信息
                    Cursor cursor = cursorLoader.loadInBackground();
                    //如果查询到联系人
                    if (cursor.moveToFirst()) {
                        //获取ID
                        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        //获取联系人名字
                        String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        String phoneNUmber = "此联系人暂未输入手机号码 ";
                        //根据联系人查询详细信息
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +
                                        " = " + contactId, null, null
                        );
                        if (phones.moveToFirst()){
                            //取出电话号码
                            phoneNUmber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        //关闭游标
                        phones.close();
                        eTxtName.setText(contactName);
                        eTxtNum.setText(phoneNUmber);
                    }
                    cursor.close();
                }
                break;
        }
    }


}
